const startButton = document.getElementById("startButton");
const stopButton = document.getElementById("stopButton");
const screenShot = document.getElementById("screenShot");

// 初始化录制状态
chrome.storage.local.get("recording", function (data) {
  if (data.recording) {
    startButton.style.display = "none";
    stopButton.style.display = "inline-block";
  } else {
    startButton.style.display = "inline-block";
    stopButton.style.display = "none";
  }
});
// 发送消息以开始录制
startButton.addEventListener("click", function () {
  chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
    // alert(`发送消息${JSON.stringify(tabs[0].title)}`);
    chrome.storage.local.set({ currentTab: tabs[0].id });
    chrome.tabs.sendMessage(tabs[0].id, { action: "startRecording" }, () => {
      if (chrome.runtime.lastError) return;
      startButton.style.display = "none";
      stopButton.style.display = "inline-block";
    });
  });
});
// 发送消息以停止录制
stopButton.addEventListener("click", function () {
  chrome.storage.local.get("currentTab", ({ currentTab }) => {
    if (!currentTab) return;
    chrome.tabs.sendMessage(currentTab, { action: "stopRecording" }, () => {
      if (chrome.runtime.lastError) return;
      stopButton.style.display = "none";
      startButton.style.display = "inline-block";
    });
  });
});
// 发送消息以截图
screenShot.addEventListener("click", function () {
  chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
    chrome.tabs.sendMessage(tabs[0].id, { action: "screenShot" }, () => {
      if (chrome.runtime.lastError) return;
    });
  });
});
